﻿using CommonType;
using Dapper;
using MediatR;
using Org.BouncyCastle.Asn1.Ocsp;
using S_Incident.Domains.WorkflowEngine;
using S_Incident.Infrasturctre;
using S_Incident.Infrasturctre.DapperInterface;
using S_Incident.Read.API.Application.Commands;
using S_Userinfor.Domains;
using System.Data;

namespace S_Incident.Read.API.Application.CommandsHanders
{
    public class FormShowCommandHanders : IRequestHandler<FormShowCommand, PageResule<FormManagement>>
    {
        private readonly IDapperFormManagementRepository repository;
        public FormShowCommandHanders(IDapperFormManagementRepository repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 表单管理列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageResule<FormManagement>> Handle(FormShowCommand request, CancellationToken cancellationToken)
        {

            var query = $" SELECT * FROM FormManagement WHERE 1=1";

            if (!string.IsNullOrEmpty(request.formName))
            {
                query += $" AND FormName LIKE '%{request.formName}%'";
            }
            var totalCount = await repository.SelectAsync(request.pageSize, query);

            query += $" ORDER BY Id DESC  LIMIT {(request.pageIndex - 1) * request.pageSize},@PageSize;";
            var List = await repository.SelectAsync(request.pageSize, query);

            return new PageResule<FormManagement>
            {
                TotalCount = totalCount.Count(),
                PageCount = (int)Math.Ceiling((totalCount.Count() * 1.0) / request.pageSize),
                Data = List,
            };

        }
           

    }
}
